package com.vader.admin.controller;

import com.vader.admin.entity.DO.BaseGroupInfo;
import com.vader.admin.entity.Query.BaseGroupInfoQuery;
import com.vader.admin.service.BaseGroupInfoService;
import com.vader.common.entity.CommonResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 组织详情表(BaseGroupInfo)表控制层
 *
 * @author Vader
 * @since 2021-10-06 12:46:38
 */
@RestController
@RequestMapping("baseGroupInfo")
public class BaseGroupInfoController {
    /**
     * 服务对象
     */
    @Resource
    private BaseGroupInfoService baseGroupInfoService;

    /**
     * 分页查询
     *
     * @param baseGroupInfo 筛选条件
     * @param pageRequest   分页对象
     * @return 查询结果
     */
    @GetMapping
    public CommonResponse<Page<BaseGroupInfo>> queryByPage(BaseGroupInfo baseGroupInfo, PageRequest pageRequest) {
        return CommonResponse.success(this.baseGroupInfoService.queryByPage(baseGroupInfo, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryGroupById/{id}")
    public CommonResponse<BaseGroupInfo> queryById(@PathVariable("id") Long id) {
        return CommonResponse.success(this.baseGroupInfoService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param baseGroupInfoQuery 实体
     * @return 新增结果
     */
    @PostMapping("/addGroup")
    public CommonResponse<BaseGroupInfo> addGroup(@RequestBody @Valid BaseGroupInfoQuery baseGroupInfoQuery) {
        BaseGroupInfo baseGroupInfo = BaseGroupInfo.from(baseGroupInfoQuery);
        return CommonResponse.success(this.baseGroupInfoService.insert(baseGroupInfo));
    }

    /**
     * 编辑组织数据
     *
     * @param baseGroupInfoQuery 实体
     * @return 编辑结果
     */
    @PutMapping("/editGroup")
    public CommonResponse<BaseGroupInfo> editGroup(@RequestBody @Valid BaseGroupInfoQuery baseGroupInfoQuery) {
        BaseGroupInfo baseGroupInfo = BaseGroupInfo.from(baseGroupInfoQuery);
        return CommonResponse.success(this.baseGroupInfoService.update(baseGroupInfo));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public CommonResponse<Boolean> deleteById(Long id) {
        return CommonResponse.success(this.baseGroupInfoService.deleteById(id));
    }

}

